package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.College;
import service.BackCollegeService;
import serviceImpl.BackCollegeServiceImpl;





@WebServlet("/BackCollegeServlet")
public class BackCollegeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public BackCollegeServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String mark = request.getParameter("mark");
		switch(mark) {
		
		case "showCollege": 
			showCollege(request,response);
			break;
		case "addCollege": 
			addCollege(request,response);
			break;
		case "modifyCollege": 
			modifyCollege(request,response);
			break;
		case "delCollege": 
			delCollege(request,response);
			break;
		
		}
	}


	private void delCollege(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int collegeid = Integer.parseInt(request.getParameter("collegeid"));
		System.out.println("collegeid:"+collegeid);
		BackCollegeService bcs = new BackCollegeServiceImpl();
		boolean bo = bcs.delCollege(collegeid);
		System.out.println(bo);
		
		PrintWriter writer = response.getWriter();
		writer.print(bo);
		
	}

	private void modifyCollege(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		int collegeid = Integer.parseInt(request.getParameter("collegeid"));
		int collegeProvince = Integer.parseInt(request.getParameter("collegeProvince"));
		String collegeName = request.getParameter("collegeName");
		String collegeLogo = request.getParameter("collegeLogo");
		System.out.println("collegeLogo:"+collegeLogo);
		
		BackCollegeService bcs = new BackCollegeServiceImpl();
		boolean bo = bcs.modifyCollege(collegeid,collegeName,collegeProvince,collegeLogo);
		System.out.println(bo);
		
		PrintWriter writer = response.getWriter();
		writer.print(bo);
		
	}

	private void addCollege(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		int cProvince = Integer.parseInt(request.getParameter("cProvince"));
		String cName = request.getParameter("cName");
		String cLogo = request.getParameter("cLogo");
		
		BackCollegeService bcs = new BackCollegeServiceImpl();
		boolean suc = bcs.addCollege(cProvince,cName,cLogo);
		System.out.println("suc:"+suc);
		PrintWriter writer = response.getWriter();
		writer.print(suc);
		
		
	}

	private void showCollege(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		BackCollegeService bcs = new BackCollegeServiceImpl();
		ArrayList<College> cList = new ArrayList<College>();
		cList = bcs.getCollege();
		request.setAttribute("collegeList", cList);
		request.getRequestDispatcher("/background/ordUserJs/index/college.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
